package com.jt.safe.ui.parent.fragment

import android.graphics.Color
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.Tab
import com.jt.safe.R
import com.jt.safe.base.BaseFragment
import com.jt.safe.ui.adpter.MessageViewPagerAdapter
import com.jt.safe.viewmodel.NullViewModel
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.fragment_parent_message.*
import kotlinx.android.synthetic.main.home_title_bar_layout.*
import kotlinx.android.synthetic.main.tab_text_widget.view.*

class ParentMessageFragment : BaseFragment<NullViewModel>() {
    val fragments = mutableListOf<Fragment>()
    val titles = mutableListOf("作业", "校服", "伙食", "通知")
    val images = mutableListOf<Int>(
        R.mipmap.notify_zy,
        R.mipmap.notify_xf,
        R.mipmap.notify_hs,
        R.mipmap.notify_tz
    )

    companion object {
        fun newInstance() = ParentMessageFragment()
    }

    override val layoutId: Int
        get() = R.layout.fragment_parent_message

    override fun initView() {
        title_tv.text = "消息通知-${titles[0]}"
        fragments.add(MessageWorkFragment.newInstance())
        fragments.add(MessageSchoolFragment.newInstance())
        fragments.add(MessageEatFragment.newInstance())
        fragments.add(MessageNotifyFragment.newInstance())
        val messageViewPagerAdapter =
            activity?.supportFragmentManager?.let { MessageViewPagerAdapter(fragments, titles, it) }
        view_pager1.adapter = messageViewPagerAdapter
        tab_layout.setupWithViewPager(view_pager1)

        for (index in 0 until tab_layout.tabCount) {
            val tabAt = tab_layout.getTabAt(index)
            val inflate = layoutInflater.inflate(R.layout.tab_text_widget, null)
            inflate.tab_title.text = titles[index]
            inflate.tab_iv.setImageResource(images[index])
            tabAt?.customView = inflate
            if (index == 0) {
                inflate.tab_title.setTextColor(resources.getColor(R.color.black))
            }
        }
        tab_layout.addOnTabSelectedListener(object : TabLayout.BaseOnTabSelectedListener<Tab> {

            override fun onTabReselected(p0: Tab) {

            }

            override fun onTabSelected(p0: Tab) {
                p0.customView!!.tab_title.setTextColor(resources.getColor(R.color.black))
            }

            override fun onTabUnselected(p0: Tab) {
                p0.customView!!.tab_title.setTextColor(Color.parseColor("#ff999999"))
            }
        })
        view_pager1.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                Logger.d(position)
                title_tv.text = "消息通知-${titles[position]}"
            }
        })
    }

    override fun createObserver() {

    }

    override fun initData() {

    }
}